#!/bin/bash
# ..............................................................................
# Title: Master_Analysis.sh
#
# Main executable file for analysis section; submits all analysis jobs via SLURM
# ..............................................................................

# email notification setting
mailtype="ALL"

# job settings
nodes=1

# second argument is job name
job=${2}

# if user has shell profile setup
if [ -f ~/shell_profile.sh ]; then

    # run profile
    source ~/shell_profile.sh

    # set user variables
    U=${whoami}
    echo "User: "${U}

    # basic variables
    system_part="<SLURM_PARTITION>"

    # paths
    code_path="<CODE_PATH>"
    erroroutput_path="$project_path/erroroutput"

    # make error output folder if it doesn't exist
    mkdir -p "${erroroutput_path}"

    # job parameters
    partition_Analysis="<SLURM_PARTITION>"
    time_Analysis="0-72:00:00"
    ntasks_Analysis=12
    mem_Analysis="120000"

else

    echo "Please set up ~/shell_profile.sh file. Exiting."
    exitcd

fi

# sanity-checking that project_path variables exist
if [[ -z "$project_path" ]]; then
    echo "Empty project_path variable: Exiting..."
    exit 1
fi


############################################################################################################################
# Submit jobs
############################################################################################################################


# Holdings sumstats step
Holdings_Sumstats_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Holdings_Sumstats \
		 --output="${erroroutput_path}/Holdings_Sumstats-%A_%a.out" --error="${erroroutput_path}/Holdings_Sumstats-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Holdings_Sumstats | awk '{print $NF}'`
echo "Submitted Holdings_Sumstats Job: "${Holdings_Sumstats_ID}
sleep 1

# Portfolio composition sumstats step
Portfolio_Composition_Sumstats_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Portfolio_Composition_Sumstats \
		 --output="${erroroutput_path}/Portfolio_Composition_Sumstats-%A_%a.out" --error="${erroroutput_path}/Portfolio_Composition_Sumstats-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Holdings_Sumstats_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Portfolio_Composition_Sumstats | awk '{print $NF}'`
echo "Submitted Portfolio_Composition_Sumstats Job: "${Portfolio_Composition_Sumstats_ID}
sleep 1

# Coverage step
Coverage_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Coverage \
         --output="${erroroutput_path}/Coverage-%A_%a.out" --error="${erroroutput_path}/Coverage-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Portfolio_Composition_Sumstats_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Coverage | awk '{print $NF}'`
echo "Submitted Coverage Job: "${Coverage_ID}
sleep 1

# GZ events step
GZ_Events_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=GZ_Events \
         --output="${erroroutput_path}/GZ_Events-%A_%a.out" --error="${erroroutput_path}/GZ_Events-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Coverage_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} GZ_Events | awk '{print $NF}'`
echo "Submitted GZ_Events Job: "${GZ_Events_ID}
sleep 1

# Security regressions step
Security_Regressions_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Security_Regressions \
         --output="${erroroutput_path}/Security_Regressions-%A_%a.out" --error="${erroroutput_path}/Security_Regressions-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${GZ_Events_ID} \
         "${code_path}/analysis/Analysis_Python_Controller.sh" ${U} Security_Regressions | awk '{print $NF}'`
echo "Submitted Security_Regressions Job: "${Security_Regressions_ID}
sleep 1

# Sale quantities step
Sale_Quantities_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Sale_Quantities \
         --output="${erroroutput_path}/Sale_Quantities-%A_%a.out" --error="${erroroutput_path}/Sale_Quantities-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Security_Regressions_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Sale_Quantities | awk '{print $NF}'`
echo "Submitted Sale_Quantities Job: "${Sale_Quantities_ID}
sleep 1

# Flow dynamics step
Flow_Dynamics_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Flow_Dynamics \
         --output="${erroroutput_path}/Flow_Dynamics-%A_%a.out" --error="${erroroutput_path}/Flow_Dynamics-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Sale_Quantities_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Flow_Dynamics | awk '{print $NF}'`
echo "Submitted Flow_Dynamics Job: "${Flow_Dynamics_ID}
sleep 1

# Variation histograms step
Variation_Histograms_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Variation_Histograms \
         --output="${erroroutput_path}/Variation_Histograms-%A_%a.out" --error="${erroroutput_path}/Variation_Histograms-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Flow_Dynamics_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Variation_Histograms | awk '{print $NF}'`
echo "Submitted Variation_Histograms Job: "${Variation_Histograms_ID}
sleep 1

# Idiosyncrasy step
Idiosyncrasy_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Idiosyncrasy \
         --output="${erroroutput_path}/Idiosyncrasy-%A_%a.out" --error="${erroroutput_path}/Idiosyncrasy-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Variation_Histograms_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Idiosyncrasy | awk '{print $NF}'`
echo "Submitted Idiosyncrasy Job: "${Idiosyncrasy_ID}
sleep 1

# Persistence step
Persistence_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Persistence \
         --output="${erroroutput_path}/Persistence-%A_%a.out" --error="${erroroutput_path}/Persistence-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Idiosyncrasy_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Persistence | awk '{print $NF}'`
echo "Submitted Persistence Job: "${Persistence_ID}
sleep 1

# Liquidity dynamics step
Liquidity_Dynamics_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Liquidity_Dynamics \
         --output="${erroroutput_path}/Liquidity_Dynamics-%A_%a.out" --error="${erroroutput_path}/Liquidity_Dynamics-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Persistence_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Liquidity_Dynamics | awk '{print $NF}'`
echo "Submitted Liquidity_Dynamics Job: "${Liquidity_Dynamics_ID}
sleep 1

# Non-US estimates step
NonUS_Estimates_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=NonUS_Estimates \
         --output="${erroroutput_path}/NonUS_Estimates-%A_%a.out" --error="${erroroutput_path}/NonUS_Estimates-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Liquidity_Dynamics_ID} \
         "${code_path}/analysis/Analysis_Python_Controller.sh" ${U} NonUS_Estimates | awk '{print $NF}'`
echo "Submitted NonUS_Estimates Job: "${NonUS_Estimates_ID}
sleep 1

# Firm regressions step
Firm_Regressions_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Firm_Regressions \
         --output="${erroroutput_path}/Firm_Regressions-%A_%a.out" --error="${erroroutput_path}/Firm_Regressions-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${NonUS_Estimates_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Firm_Regressions | awk '{print $NF}'`
echo "Submitted Firm_Regressions Job: "${Firm_Regressions_ID}
sleep 1

# Estimates plot step
Plot_Estimates_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Plot_Estimates \
         --output="${erroroutput_path}/Plot_Estimates-%A_%a.out" --error="${erroroutput_path}/Plot_Estimates-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Firm_Regressions_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Plot_Estimates | awk '{print $NF}'`
echo "Submitted Plot_Estimates Job: "${Plot_Estimates_ID}
sleep 1

# Relationship effects step
Relationships_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Relationships \
         --output="${erroroutput_path}/Relationships-%A_%a.out" --error="${erroroutput_path}/Relationships-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Plot_Estimates_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Relationships | awk '{print $NF}'`
echo "Submitted Relationships Job: "${Relationships_ID}
sleep 1

# Spreads analysis step
Spreads_ID=`sbatch \
         --partition=${partition_Analysis} --time=${time_Analysis} \
         --nodes=${nodes} --ntasks=${ntasks_Analysis} --job-name=Spreads \
         --output="${erroroutput_path}/Spreads-%A_%a.out" --error="${erroroutput_path}/Spreads-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Analysis} \
         --depend=afterok:${Spreads_ID} \
         "${code_path}/analysis/Analysis_Stata_Controller.sh" ${U} Spreads | awk '{print $NF}'`
echo "Submitted Spreads Job: "${Spreads_ID}
sleep 1

echo "All jobs submitted!"
